package com.king.mvpdemo.newfragment.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.king.mvpdemo.R;
import com.king.mvpdemo.newfragment.bean.ListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author King
 * @Date 2019/8/8 16:33
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<ListBean.ResultBean.DataBean> mList;

    public RecyclerViewAdapter(Context context, List<ListBean.ResultBean.DataBean> list) {
        this.mContext = context;
        this.mList = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        if(i==1){
            return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.item, null));
        }else if(i==2){
            return new MyHolder2(LayoutInflater.from(mContext).inflate(R.layout.item2, null));
        }else{
            return new MyHolder3(LayoutInflater.from(mContext).inflate(R.layout.item3, null));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyHolder) {
            MyHolder myHolder = (MyHolder)holder;
            myHolder.tvName.setText(mList.get(position).getTitle());
            myHolder.tvDate.setText("时间：" + mList.get(position).getDate());
            myHolder.tvAuthorName.setText("来源：" + mList.get(position).getAuthor_name());
            Glide.with(mContext).load(mList.get(position).getThumbnail_pic_s()).placeholder(R.drawable.zhanweitu)
                    .error(R.drawable.jiazaishibai).into(myHolder.ivOne);
            Glide.with(mContext).load(mList.get(position).getThumbnail_pic_s02()).placeholder(R.drawable.zhanweitu)
                    .error(R.drawable.jiazaishibai).into(myHolder.ivTow);
            Glide.with(mContext).load(mList.get(position).getThumbnail_pic_s03()).placeholder(R.drawable.zhanweitu)
                    .error(R.drawable.jiazaishibai).into(myHolder.ivThere);
            Log.e("---", "" + position + "-----" + mList.size());
            if (position ==(mList.size() - 1)) {
                myHolder.viId.setVisibility(View.GONE);
            }
        } else if (holder instanceof MyHolder2){
            MyHolder2 myHolder = (MyHolder2) holder;
            if (position == (mList.size() - 1)) {
                myHolder.vi_id2.setVisibility(View.GONE);
            }
            myHolder.text2.setText(mList.get(position).getTitle());
            myHolder.tv_date2.setText("时间：" + mList.get(position).getDate());
            myHolder.tv_author_name2.setText("来源：" + mList.get(position).getAuthor_name());
            Glide.with(mContext).load(mList.get(position).getThumbnail_pic_s()).placeholder(R.drawable.zhanweitu)
                    .error(R.drawable.jiazaishibai).into(myHolder.iv_on2);
            Glide.with(mContext).load(mList.get(position).getThumbnail_pic_s02()).placeholder(R.drawable.zhanweitu)
                    .error(R.drawable.jiazaishibai).into(myHolder.iv_tow2);
        } else if (holder instanceof MyHolder3){
            MyHolder3 myHolder = (MyHolder3) holder;
            if (position ==  (mList.size() - 1)) {
                myHolder.vi_id3.setVisibility(View.GONE);
            }
            myHolder.text3.setText(mList.get(position).getTitle());
            myHolder.tv_date3.setText("时间：" + mList.get(position).getDate());
            myHolder.tv_author_name3.setText("来源：" + mList.get(position).getAuthor_name());
            Glide.with(mContext).load(mList.get(position).getThumbnail_pic_s()).placeholder(R.drawable.zhanweitu)
                    .error(R.drawable.jiazaishibai).into(myHolder.iv_on3);
        }

        /*holder.itemView.setOnClickListener {
            itemClickListener !!.onItemClickListener(listData.get(position))
        }*/
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (!TextUtils.isEmpty(mList.get(position).getThumbnail_pic_s()) && !TextUtils.isEmpty(mList.get(position).getThumbnail_pic_s02()) && !TextUtils.isEmpty(
                mList.get(position).getThumbnail_pic_s03())) {
            position = 1;
        } else if (!TextUtils.isEmpty(mList.get(position).getThumbnail_pic_s()) && !TextUtils.isEmpty(
                mList.get(position).getThumbnail_pic_s02())) {
            position = 2;
        } else if (!TextUtils.isEmpty(mList.get(position).getThumbnail_pic_s())) {
            position = 3;
        }

        return position;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
           /* TextView text = itemView.findViewById(R.id.tv_name);
            TextView tv_author_name = itemView.findViewById(R.id.tv_author_name);
            TextView tv_date = itemView.findViewById(R.id.tv_date);
            ImageView iv_on = itemView.findViewById(R.id.iv_one);
            ImageView iv_tow = itemView.findViewById(R.id.iv_tow);
            ImageView iv_there = itemView.findViewById(R.id.iv_there);
            View vi_id = itemView.findViewById(R.id.vi_id);*/
        }
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.iv_one)
        ImageView ivOne;
        @BindView(R.id.iv_tow)
        ImageView ivTow;
        @BindView(R.id.iv_there)
        ImageView ivThere;
        @BindView(R.id.tv_author_name)
        TextView tvAuthorName;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.vi_id)
        View viId;
    }

    class MyHolder2 extends RecyclerView.ViewHolder {
        public MyHolder2(@NonNull View itemView) {
            super(itemView);

        }
        TextView text2 = itemView.findViewById(R.id.tv_name);
        TextView tv_author_name2 = itemView.findViewById(R.id.tv_author_name);
        TextView tv_date2 = itemView.findViewById(R.id.tv_date);
        ImageView iv_on2 = itemView.findViewById(R.id.iv_one);
        ImageView iv_tow2 = itemView.findViewById(R.id.iv_tow);
        View vi_id2 = itemView.findViewById(R.id.vi_id);
    }

    class MyHolder3 extends RecyclerView.ViewHolder {
        public MyHolder3(@NonNull View itemView) {
            super(itemView);
        }
        TextView text3 = itemView.findViewById(R.id.tv_name);
        TextView tv_author_name3 = itemView.findViewById(R.id.tv_author_name);
        TextView tv_date3 = itemView.findViewById(R.id.tv_date);
        ImageView iv_on3 = itemView.findViewById(R.id.iv_one);
        View vi_id3 = itemView.findViewById(R.id.vi_id);
    }
}
